1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /**
18  * @addtogroup Networking
19  * @{
20  */
21 
22 /**
23  * @file multinetwork.h
24  */
25 
26 module android.ndk.multinetwork;
27 
28 import core.stdc.config;
29 import core.sys.posix.netdb;
30 
31 import arsd.jni;
32 import android.ndk;
33 
34 extern (C):
35 nothrow:
36 @nogc:
37 
38 /**
39  * The corresponding C type for android.net.Network#getNetworkHandle() return
40  * values.  The Java signed long value can be safely cast to a net_handle_t:
41  *
42  *     [C]    ((net_handle_t) java_long_network_handle)
43  *     [C++]  static_cast<net_handle_t>(java_long_network_handle)
44  *
45  * as appropriate.
46  */
47 alias net_handle_t = c_ulong;
48 
49 /**
50  * The value NETWORK_UNSPECIFIED indicates no specific network.
51  *
52  * For some functions (documented below), a previous binding may be cleared
53  * by an invocation with NETWORK_UNSPECIFIED.
54  *
55  * Depending on the context it may indicate an error.  It is expressly
56  * not used to indicate some notion of the "current default network".
57  */
58 enum NETWORK_UNSPECIFIED = cast(net_handle_t) 0;
59 
60 /**
61  * All functions below that return an int return 0 on success or -1
62  * on failure with an appropriate errno value set.
63  */
64 
65 /**
66  * Set the network to be used by the given socket file descriptor.
67  *
68  * To clear a previous socket binding, invoke with NETWORK_UNSPECIFIED.
69  *
70  * This is the equivalent of: [android.net.Network#bindSocket()](https://developer.android.com/reference/android/net/Network.html#bindSocket(java.net.Socket))
71  *
72  */
73 int android_setsocknetwork (net_handle_t network, int fd);
74 
75 /**
76  * Binds the current process to |network|.  All sockets created in the future
77  * (and not explicitly bound via android_setsocknetwork()) will be bound to
78  * |network|.  All host name resolutions will be limited to |network| as well.
79  * Note that if the network identified by |network| ever disconnects, all
80  * sockets created in this way will cease to work and all host name
81  * resolutions will fail.  This is by design so an application doesn't
82  * accidentally use sockets it thinks are still bound to a particular network.
83  *
84  * To clear a previous process binding, invoke with NETWORK_UNSPECIFIED.
85  *
86  * This is the equivalent of: [android.net.ConnectivityManager#setProcessDefaultNetwork()](https://developer.android.com/reference/android/net/ConnectivityManager.html#setProcessDefaultNetwork(android.net.Network))
87  *
88  */
89 int android_setprocnetwork (net_handle_t network);
90 
91 /**
92  * Perform hostname resolution via the DNS servers associated with |network|.
93  *
94  * All arguments (apart from |network|) are used identically as those passed
95  * to getaddrinfo(3).  Return and error values are identical to those of
96  * getaddrinfo(3), and in particular gai_strerror(3) can be used as expected.
97  * Similar to getaddrinfo(3):
98  *     - |hints| may be NULL (in which case man page documented defaults apply)
99  *     - either |node| or |service| may be NULL, but not both
100  *     - |res| must not be NULL
101  *
102  * This is the equivalent of: [android.net.Network#getAllByName()](https://developer.android.com/reference/android/net/Network.html#getAllByName(java.lang.String))
103  *
104  */
105 int android_getaddrinfofornetwork (
106     net_handle_t network,
107     const(char)* node,
108     const(char)* service,
109     const(addrinfo)* hints,
110     addrinfo** res);
111 
112 /* __ANDROID_API__ >= 23 */
113 
114 /**
115  * Possible values of the flags argument to android_res_nsend and android_res_nquery.
116  * Values are ORed together.
117  */
118 enum ResNsendFlags
119 {
120     /**
121      * Send a single request to a single resolver and fail on timeout or network errors
122      */
123     ANDROID_RESOLV_NO_RETRY = 1 << 0,
124 
125     /**
126      * Do not cache the result of the lookup. The lookup may return a result that is already
127      * in the cache, unless the ANDROID_RESOLV_NO_CACHE_LOOKUP flag is also specified.
128      */
129     ANDROID_RESOLV_NO_CACHE_STORE = 1 << 1,
130 
131     /**
132      * Don't lookup the request in cache.
133      */
134     ANDROID_RESOLV_NO_CACHE_LOOKUP = 1 << 2
135 }
136 
137 /**
138  * Look up the {|ns_class|, |ns_type|} Resource Record (RR) associated
139  * with Domain Name |dname| on the given |network|.
140  * The typical value for |ns_class| is ns_c_in, while |type| can be any
141  * record type (for instance, ns_t_aaaa or ns_t_txt).
142  * |flags| is a additional config to control actual querying behavior, see
143  * ResNsendFlags for detail.
144  *
145  * Returns a file descriptor to watch for read events, or a negative
146  * POSIX error code (see errno.h) if an immediate error occurs.
147  */
148 int android_res_nquery (
149     net_handle_t network,
150     const(char)* dname,
151     int ns_class,
152     int ns_type,
153     uint flags);
154 
155 /**
156  * Issue the query |msg| on the given |network|.
157  * |flags| is a additional config to control actual querying behavior, see
158  * ResNsendFlags for detail.
159  *
160  * Returns a file descriptor to watch for read events, or a negative
161  * POSIX error code (see errno.h) if an immediate error occurs.
162  */
163 int android_res_nsend (
164     net_handle_t network,
165     const(ubyte)* msg,
166     size_t msglen,
167     uint flags);
168 
169 /**
170  * Read a result for the query associated with the |fd| descriptor.
171  * Closes |fd| before returning.
172  *
173  * Returns:
174  *     < 0: negative POSIX error code (see errno.h for possible values). |rcode| is not set.
175  *     >= 0: length of |answer|. |rcode| is the resolver return code (e.g., ns_r_nxdomain)
176  */
177 int android_res_nresult (int fd, int* rcode, ubyte* answer, size_t anslen);
178 
179 /**
180  * Attempts to cancel the in-progress query associated with the |nsend_fd|
181  * descriptor.
182  */
183 void android_res_cancel (int nsend_fd);
184 
185 /* __ANDROID_API__ >= 29 */
186 
187 // ANDROID_MULTINETWORK_H
188 
189 /** @} */